package com.lostreseditores.sabersai.dataaccess.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Example;

import com.lostreseditores.sabersai.dataaccess.sessionFactory.HibernateSessionFactory;
import com.lostreseditores.sabersai.model.Calificacionxestudiante;


public class CalificacionxestudianteDAO implements ICalificacionxestudianteDAO {
    private static final Log log = LogFactory.getLog(CalificacionxestudianteDAO.class);

    // property constants
    //public static final Integer  CALIACTIVON = "caliactivon";
    public static final String CALIACTIVON = "caliactivon";

    //public static final Integer  CALICODAUXN = "calicodauxn";
    public static final String CALICODAUXN = "calicodauxn";

    //public static final Integer  CALIPREORDEN = "calipreorden";
    public static final String CALIPREORDEN = "calipreorden";

    //public static final Integer  CALIXESTUIDN = "calixestuidn";
    public static final String CALIXESTUIDN = "calixestuidn";

    //public static final String  CALIXESTURESPV = "calixesturespv";
    public static final String CALIXESTURESPV = "calixesturespv";

    private Session getSession() {
        return HibernateSessionFactory.getSession();
    }

    /**
    *
    * @param Instance
    *            Calificacionxestudiante Instance to persist
    * @throws RuntimeException
    *             when the operation fails
    */
    public void save(Calificacionxestudiante instance) {
        log.debug("saving Calificacionxestudiante instance");

        try {
            getSession().save(instance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    /**
    * @param Instance
    *            Calificacionxestudiante Instance to delete
    * @throws RuntimeException
    *             when the operation fails
    */
    public void delete(Calificacionxestudiante instance) {
        log.debug("deleting Calificacionxestudiante instance");

        try {
            getSession().delete(instance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    /**
    *
    * @param Instance
    *            Calificacionxestudiante Instance to update
    * @throws RuntimeException
    *             when the operation fails
    */
    public void update(Calificacionxestudiante instance) {
        log.debug("updating Calificacionxestudiante instance");

        try {
            getSession().update(instance);
            log.debug("update successful");
        } catch (RuntimeException re) {
            log.error("update failed", re);
            throw re;
        }
    }

    public Calificacionxestudiante findById(Integer id) {
        log.debug("finding Calificacionxestudiante instance with id: " + id);

        try {
            Calificacionxestudiante instance = (Calificacionxestudiante) getSession()
                                                                             .get(Calificacionxestudiante.class,
                    id);

            return instance;
        } catch (RuntimeException re) {
            log.error("finding Calificacionxestudiante failed", re);
            throw re;
        }
    }

    public List<Calificacionxestudiante> findByExample(
        Calificacionxestudiante instance) {
        log.debug("finding Calificacionxestudiante instance by example");

        try {
            List results = getSession().createCriteria("Calificacionxestudiante")
                               .add(Example.create(instance)).list();
            log.debug("find by example successful, result size: " +
                results.size());

            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }

    /**
    * Find all  Calificacionxestudiante entities with a specific property value.
    *
    * @param value
    *            the property value to match
    * @param propertyName
    *            the property to search in the instance
    * @return List< Calificacionxestudiante> found by query
        */
    public List<Calificacionxestudiante> findByProperty(String propertyName,
        Object value) {
        log.debug("finding Calificacionxestudiante instance with property: " +
            propertyName + ", value: " + value);

        try {
            String queryString = "from Calificacionxestudiante as model where model." +
                propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List<Calificacionxestudiante> findByCaliactivon(Object caliactivon) {
        return findByProperty(CALIACTIVON, caliactivon);
    }

    public List<Calificacionxestudiante> findByCalicodauxn(Object calicodauxn) {
        return findByProperty(CALICODAUXN, calicodauxn);
    }

    public List<Calificacionxestudiante> findByCalipreorden(Object calipreorden) {
        return findByProperty(CALIPREORDEN, calipreorden);
    }

    public List<Calificacionxestudiante> findByCalixestuidn(Object calixestuidn) {
        return findByProperty(CALIXESTUIDN, calixestuidn);
    }

    public List<Calificacionxestudiante> findByCalixesturespv(
        Object calixesturespv) {
        return findByProperty(CALIXESTURESPV, calixesturespv);
    }

    /**
    * Find all Calificacionxestudiante entities.
    *
    * @return List<Calificacionxestudiante> all Calificacionxestudiante instances
    */
    public List<Calificacionxestudiante> findAll() {
        log.debug("finding all Calificacionxestudiante instances");

        try {
            String queryString = "from Calificacionxestudiante";
            Query queryObject = getSession().createQuery(queryString);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public List<Calificacionxestudiante> findByCriteria(String whereCondition) {
        log.debug("finding Calificacionxestudiante " + whereCondition);

        try {
            String where = ((whereCondition == null) ||
                (whereCondition.length() == 0)) ? "" : ("where " +
                whereCondition);
            final String queryString = "select model from Calificacionxestudiante model " +
                where;
            Query query = getSession().createQuery(queryString);
            List<Calificacionxestudiante> entitiesList = query.list();

            return entitiesList;
        } catch (RuntimeException re) {
            log.error("find By Criteria in Calificacionxestudiante failed", re);
            throw re;
        }
    }

    public List<Calificacionxestudiante> findPageCalificacionxestudiante(
        String sortColumnName, boolean sortAscending, int startRow,
        int maxResults) {
        log.debug(
            "finding Calificacionxestudiante findPageCalificacionxestudiante");

        if ((sortColumnName != null) && (sortColumnName.length() > 0)) {
            try {
                String queryString = "select model from Calificacionxestudiante model order by model." +
                    sortColumnName + " " + (sortAscending ? "asc" : "desc");

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        } else {
            try {
                String queryString = "select model from Calificacionxestudiante model";

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        }
    }

    public Long findTotalNumberCalificacionxestudiante() {
        log.debug("finding Calificacionxestudiante count");

        try {
            String queryString = "select count(*) from Calificacionxestudiante model";
            Query queryObject = getSession().createQuery(queryString);

            return (Long) queryObject.list().get(0);
        } catch (RuntimeException re) {
            throw re;
        }
    }
    
	public List<Calificacionxestudiante> findEstudiantesporCalificacion(
			Integer caliidn) {
		log.debug("finding all Calificacionxestudiante instances");

		try {
			String queryString = "SELECT DISTINCT estudiante.estuidn as estuidn, estunombrev FROM calificacionxestudiante INNER JOIN (estudiante INNER JOIN genero ON estudiante.geneidn = genero.geneidn) ON calificacionxestudiante.estuidn = estudiante.estuidn WHERE calificacionxestudiante.caliidn ="
					+ caliidn + " order by estunombrev asc";

			Query queryObject = getSession().createSQLQuery(queryString);

			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

}
